/*
 * @FileName: [ComboCardServiceImpl.java]
 * 
 * @Package com.bcinfo.iccard.service.impl
 * 
 * 
 * Copyright (c) 2011-2015 BCINFO Technology Limited Com.
 * All rights reserved.
 * 
 * This software is the confidential and proprietnewCardNums
 * information of BCINFO Technology Limited Company
 * ("Confidential Information"). You shall not disclose
 * such Confidential Information and shall use it only
 * in accordance with the terms of the contract agreement
 * you entered into with RKY.
 * 
 * $Rev: 776 $
 * $LastChangedDate$
 * $LastChangedBy: huangb $
 * 
 * @category ICCARD
 * 
 * @version 1.1
 * 
 * @author $Author: huangb $
 * 
 * Change History:[Formatter: author date description] <br/>
 * 1
 * 2
 * 3
 */

package com.bcinfo.iccard.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bcinfo.iccard.dao.CheckAccountLogMapper;
import com.bcinfo.iccard.dao.ComboCardCheckreasonMapper;
import com.bcinfo.iccard.dao.ComboCardMapper;
import com.bcinfo.iccard.dao.ComboCardProductDetailMapper;
import com.bcinfo.iccard.dao.ComboCardSaleDetailMapper;
import com.bcinfo.iccard.dao.ExpenseRecordMapper;
import com.bcinfo.iccard.dao.MerchantMapper;
import com.bcinfo.iccard.exception.ComboCardNameIsExistException;
import com.bcinfo.iccard.exception.IccardDatabaseException;
import com.bcinfo.iccard.exception.InvalidParametersException;
import com.bcinfo.iccard.model.CheckAccountLog;
import com.bcinfo.iccard.model.ComboCard;
import com.bcinfo.iccard.model.ComboCardCheckreason;
import com.bcinfo.iccard.model.ComboCardProductDetail;
import com.bcinfo.iccard.model.ComboCardSaleDetail;
import com.bcinfo.iccard.model.Merchant;
import com.bcinfo.iccard.service.ComboCardService;
import com.bcinfo.iccard.util.PaginationList;

/**
 * 一句话描述 <br/>
 * <p>
 * Description:<br/>
 * <p>
 * TODO 详细描述
 * 
 * <p>
 * For Examples <br/>
 * 
 * <PRE>
 *      TODO 代码使用示例
 * </PRE>
 * <p>
 */
@Service("comboCardService")
public class ComboCardServiceImpl implements ComboCardService
{

    private ComboCardMapper              _comboCardMapper;
    private ComboCardProductDetailMapper _comboCardProductDetailMapper;
    private MerchantMapper               _merchantMapper;
    private ComboCardCheckreasonMapper   _comboCardCheckreasonMapper;
    private ComboCardSaleDetailMapper    _comboCardSaleDetailMapper;
    private ExpenseRecordMapper          _expenseRecordMapper;
    private CheckAccountLogMapper        _checkAccountLogMapper;
    private SqlSessionTemplate           batchSqlSession;

    /**
     * @return the batchSqlSession
     */
    public SqlSessionTemplate getBatchSqlSession()
    {
        return batchSqlSession;
    }

    /**
     * @param batchSqlSession
     *            the batchSqlSession to set
     */
    public void setBatchSqlSession(SqlSessionTemplate batchSqlSession)
    {
        this.batchSqlSession = batchSqlSession;
    }

    /**
     * @return the _comboCardMapper
     */
    public ComboCardMapper get_comboCardMapper()
    {
        return _comboCardMapper;
    }

    /**
     * @param _comboCardMapper
     *            the _comboCardMapper to set
     */
    public void set_comboCardMapper(ComboCardMapper _comboCardMapper)
    {
        this._comboCardMapper = _comboCardMapper;
    }

    /**
     * @return the _comboCardProductDetailMapper
     */
    public ComboCardProductDetailMapper get_comboCardProductDetailMapper()
    {
        return _comboCardProductDetailMapper;
    }

    /**
     * @param _comboCardProductDetailMapper
     *            the _comboCardProductDetailMapper to set
     */
    public void set_comboCardProductDetailMapper(ComboCardProductDetailMapper _comboCardProductDetailMapper)
    {
        this._comboCardProductDetailMapper = _comboCardProductDetailMapper;
    }

    /**
     * @return the _merchantMapper
     */
    public MerchantMapper get_merchantMapper()
    {
        return _merchantMapper;
    }

    /**
     * @param _merchantMapper
     *            the _merchantMapper to set
     */
    public void set_merchantMapper(MerchantMapper _merchantMapper)
    {
        this._merchantMapper = _merchantMapper;
    }

    /**
     * @return the _comboCardCheckreasonMapper
     */
    public ComboCardCheckreasonMapper get_comboCardCheckreasonMapper()
    {
        return _comboCardCheckreasonMapper;
    }

    /**
     * @param _comboCardCheckreasonMapper
     *            the _comboCardCheckreasonMapper to set
     */
    public void set_comboCardCheckreasonMapper(ComboCardCheckreasonMapper _comboCardCheckreasonMapper)
    {
        this._comboCardCheckreasonMapper = _comboCardCheckreasonMapper;
    }

    /**
     * @return the _comboCardSaleDetailMapper
     */
    public ComboCardSaleDetailMapper get_comboCardSaleDetailMapper()
    {
        return _comboCardSaleDetailMapper;
    }

    /**
     * @param _comboCardSaleDetailMapper
     *            the _comboCardSaleDetailMapper to set
     */
    public void set_comboCardSaleDetailMapper(ComboCardSaleDetailMapper _comboCardSaleDetailMapper)
    {
        this._comboCardSaleDetailMapper = _comboCardSaleDetailMapper;
    }

    /**
     * @return the _expenseRecordMapper
     */
    public ExpenseRecordMapper get_expenseRecordMapper()
    {
        return _expenseRecordMapper;
    }

    /**
     * @param _expenseRecordMapper
     *            the _expenseRecordMapper to set
     */
    public void set_expenseRecordMapper(ExpenseRecordMapper _expenseRecordMapper)
    {
        this._expenseRecordMapper = _expenseRecordMapper;
    }

    /**
     * @return the _checkAccountLogMapper
     */
    public CheckAccountLogMapper get_checkAccountLogMapper()
    {
        return _checkAccountLogMapper;
    }

    /**
     * @param _checkAccountLogMapper the _checkAccountLogMapper to set
     */
    public void set_checkAccountLogMapper(CheckAccountLogMapper _checkAccountLogMapper)
    {
        this._checkAccountLogMapper = _checkAccountLogMapper;
    }

    private static final Logger _logger = Logger.getLogger(ComboCardServiceImpl.class);

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param comboCard
     * 
     * @param opId
     * 
     * @return
     * 
     * @throws IccardDatabaseException
     * 
     * @throws CardNumIsExistException
     * 
     * @see com.bcinfo.iccard.service.ComboCardService#add(com.bcinfo.iccard.model.ComboCard, long)
     */
    @Override
    public int add(ComboCard comboCard) throws IccardDatabaseException, ComboCardNameIsExistException
    {
        if (_logger.isDebugEnabled())
        {
            _logger.debug("comboCard=" + comboCard.toString());
        }
        int count = 0;
        try
        {
            count = _comboCardMapper.selectByCardName(comboCard.getComboCardName());
        }
        catch (DataAccessException e1)
        {
            _logger.error(ExceptionUtils.getStackTrace(e1));
            throw new IccardDatabaseException();
        }
        if (count > 0)
        {
            _logger.error("add:comboCard=" + comboCard + " is exist");
            throw new ComboCardNameIsExistException();
        }
        int result = 0;
        try
        {
            result = _comboCardMapper.insertSelective(comboCard);

            if (result > 0 && comboCard.getComboCardProductDetails() != null
                    && comboCard.getComboCardProductDetails().size() > 0)
            {
                SqlSession session = batchSqlSession.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
                ComboCardProductDetailMapper comboCardProductDetailMapper = session
                        .getMapper(ComboCardProductDetailMapper.class);
                for (ComboCardProductDetail comboCardProductDetail : comboCard.getComboCardProductDetails())
                {
                    comboCardProductDetail.setComboCardId(comboCard.getComboCardId());
                    comboCardProductDetailMapper.insertSelective(comboCardProductDetail);
                }
                session.commit();
                session.clearCache();
            }
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new IccardDatabaseException();
        }
        return result;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param comboCardName
     * 
     * @param status
     * 
     * @param pageSize
     * 
     * @param pageNo
     * 
     * @return
     * 
     * @throws IccardDatabaseException
     * 
     * @throws InvalidParametersException
     * 
     * @see com.bcinfo.iccard.service.ComboCardService#find(java.lang.String, int, int, int)
     */
    @Override
    public PaginationList<ComboCard> find(String comboCardName, int status, int pageSize, int pageNo)
            throws IccardDatabaseException, InvalidParametersException
    {
        PaginationList<ComboCard> paginationList = new PaginationList<ComboCard>();
        List<ComboCard> rechargeableCards = null;
        int totalLogCount = 0;
        System.out.println("find:,status=" + status + ",comboCardName=" + comboCardName + ",pageSize=" + pageSize
                + ",pageNo=" + pageNo);
        if (_logger.isDebugEnabled())
        {
            _logger.info("find:,status=" + status + ",comboCardName=" + comboCardName + ",pageSize=" + pageSize
                    + ",pageNo=" + pageNo);
        }
        try
        {
            totalLogCount = _comboCardMapper.selectCountByPage(comboCardName, status);
        }
        catch (Exception e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
        if (pageNo <= 0 || totalLogCount < (pageNo - 1) * pageSize || pageSize <= 0)
        {
            _logger.error("parameter is wrong");
            throw new InvalidParametersException();
        }
        int benginIndex = pageSize * pageNo - pageSize;
        try
        {
            rechargeableCards = _comboCardMapper.selectByPage(comboCardName, status, benginIndex, pageSize);
        }
        catch (Exception e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }

        int totalPage = totalLogCount % pageSize == 0 ? totalLogCount / pageSize : totalLogCount / pageSize + 1;
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageNo);
        paginationList.setPageSize(pageSize);
        paginationList.setRecords(rechargeableCards);
        paginationList.setTotalRecord(totalLogCount);
        return paginationList;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param comboCardId
     * 
     * @param opId
     * 
     * @return
     * 
     * @throws IccardDatabaseException
     * 
     * @see com.bcinfo.iccard.service.ComboCardService#delete(long, long)
     */
    @Override
    public int delete(long comboCardId) throws IccardDatabaseException
    {
        int result = 0;
        if (_logger.isInfoEnabled())
        {
            _logger.info("delete:comboCardId=" + comboCardId);
        }
        try
        {
            result = _comboCardMapper.deleteByPrimaryKey(comboCardId);
            if (result > 0)
            {
                _comboCardProductDetailMapper.deleteByCardId(comboCardId);
            }
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
        return result;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param comboCard
     * 
     * @return
     * 
     * @throws AlumniDatabaseException
     * 
     * @see com.bcinfo.iccard.service.ComboCardService#update(com.bcinfo.iccard.model.ComboCard)
     */
    @Override
    public int update(ComboCard comboCard) throws IccardDatabaseException, InvalidParametersException
    {
        if (_logger.isInfoEnabled())
        {
            _logger.info("update:comboCard=" + comboCard.toString());
        }
        if (comboCard == null)
        {
            _logger.error("update parameter is wrong :comboCard is null");
            throw new InvalidParametersException();
        }
        try
        {
            int resultCount = _comboCardMapper.updateByPrimaryKeySelective(comboCard);

            if (resultCount > 0 && comboCard.getComboCardProductDetails() != null
                    && comboCard.getComboCardProductDetails().size() > 0)
            {
                _comboCardProductDetailMapper.deleteByCardId(comboCard.getComboCardId());
                SqlSession session = batchSqlSession.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
                ComboCardProductDetailMapper comboCardProductDetailMapper = session
                        .getMapper(ComboCardProductDetailMapper.class);
                for (ComboCardProductDetail comboCardProductDetail : comboCard.getComboCardProductDetails())
                {
                    comboCardProductDetail.setComboCardId(comboCard.getComboCardId());
                    comboCardProductDetailMapper.insertSelective(comboCardProductDetail);
                }
                session.commit();
                session.clearCache();
            }

            if (_logger.isDebugEnabled())
            {
                _logger.debug("update:result update count is " + resultCount);
            }
            return resultCount;
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param userId
     * 
     * @return
     * 
     * @throws AlumniDatabaseException
     * 
     * @see com.bcinfo.iccard.service.ComboCardService#load(long)
     */
    @Override
    public ComboCard load(long comboCardId) throws IccardDatabaseException
    {
        if (_logger.isInfoEnabled())
        {
            _logger.info("load:comboCardId=" + comboCardId);
        }
        ComboCard comboCard = null;
        try
        {
            comboCard = _comboCardMapper.selectByPrimaryKey(comboCardId);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
        if (comboCard != null)
        {
            _logger.debug("load:result ComboCard=" + comboCard.toString());
        }
        return comboCard;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @return
     * 
     * @throws IccardDatabaseException
     * 
     * @see com.bcinfo.iccard.service.ComboCardService#findAllMerchant()
     */
    @Override
    public List<Merchant> findAllMerchant() throws IccardDatabaseException
    {
        List<Merchant> list = null;
        if (_logger.isDebugEnabled())
        {
            _logger.info("findAllMerchant");
        }
        try
        {
            list = _merchantMapper.selectAll();
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
        return list;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @return
     * 
     * @throws IccardDatabaseException
     * 
     * @see com.bcinfo.iccard.service.ComboCardService#findAllMerchant()
     */
    @Override
    public Merchant findMerchant(long merchantId) throws IccardDatabaseException
    {
        Merchant m = null;
        if (_logger.isDebugEnabled())
        {
            _logger.info("findAllMerchant");
        }
        try
        {
            m = _merchantMapper.selectByPrimaryKey(merchantId);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
        return m;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param comboCard
     * 
     * @return
     * 
     * @throws AlumniDatabaseException
     * 
     * @see com.bcinfo.iccard.service.ComboCardService#audit(com.bcinfo.iccard.model.ComboCard)
     */
    @Override
    public int audit(long comboCardId, int status, String msg, long opId) throws IccardDatabaseException
    {
        if (_logger.isInfoEnabled())
        {
            _logger.info("audit:status=" + status + ",comboCardId=" + comboCardId + ",msg=" + msg + ",opId=" + opId);
        }
        try
        {
            ComboCard comboCard = new ComboCard();
            comboCard.setComboCardId(comboCardId);
            comboCard.setComboCardCheckStatus((short) status);
            if (status == 1)
            {
                comboCard.setIsDelete(false);
            }
            System.out.println("comboCard="+comboCard);
            int resultCount = _comboCardMapper.updateByPrimaryKeySelective(comboCard);

            ComboCardCheckreason comboCardCheckreason = new ComboCardCheckreason();
            comboCardCheckreason.setComboCardId(comboCardId);
            comboCardCheckreason.setUserId(opId);
            comboCardCheckreason.setCheckContent(msg);

            _comboCardCheckreasonMapper.insertSelective(comboCardCheckreason);

            if (_logger.isDebugEnabled())
            {
                _logger.debug("audit:result audit count is " + resultCount);
            }
            return resultCount;
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param userId
     * 
     * @return
     * 
     * @throws AlumniDatabaseException
     * 
     * @see com.bcinfo.iccard.service.ComboCardService#load(long)
     */
    @Override
    public ComboCardCheckreason loadComboCardCheckreason(long comboCardId) throws IccardDatabaseException
    {
        if (_logger.isInfoEnabled())
        {
            _logger.info("loadComboCardCheckreason:comboCardId=" + comboCardId);
        }
        ComboCardCheckreason comboCardCheckreason = null;
        try
        {
            comboCardCheckreason = _comboCardCheckreasonMapper.selectByComboCardId(comboCardId);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
        if (comboCardCheckreason != null)
        {
            _logger.debug("loadComboCardCheckreason:result comboCardCheckreason=" + comboCardCheckreason.toString());
        }
        return comboCardCheckreason;
    }
    

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param comboCardId
     * 
     * @param status
     * 
     * @param opId
     * 
     * @return
     * 
     * @throws AlumniDatabaseException
     * 
     * @see com.bcinfo.iccard.service.ComboCardService#sale(com.bcinfo.iccard.model.ComboCard)
     */
    @Override
    public ComboCardSaleDetail sale(long comboCardId,int status,long opId,String twoDimensionCode,String strQRCodePath) throws IccardDatabaseException
    {
        if (_logger.isInfoEnabled())
        {
            _logger.info("audit:status=" + status + ",comboCardId=" + comboCardId + ",opId=" + opId);
        }
        try
        {
            ComboCard comboCard = load(comboCardId);
            comboCard.setComboCardSurplusCopies(comboCard.getComboCardSurplusCopies() - 1);
            comboCard.setIsDelete(false);
            int resultCount = _comboCardMapper.updateByPrimaryKeySelective(comboCard);

            ComboCardSaleDetail comboCardSaleDetail = new ComboCardSaleDetail();
            comboCardSaleDetail.setComboCardId(comboCardId);
            comboCardSaleDetail.setUserId(opId);
            comboCardSaleDetail.setComboCardName(comboCard.getComboCardName());
            comboCardSaleDetail.setPaymentMode((short) status);
            comboCardSaleDetail.setPaymentAmount(comboCard.getComboCardSalePrice());
            comboCardSaleDetail.setExtractStatus((short) 0);
            comboCardSaleDetail.setTwoDimensionCode(twoDimensionCode);
            comboCardSaleDetail.setTwoDimensionAddr(strQRCodePath);
            _comboCardSaleDetailMapper.insertSelective(comboCardSaleDetail);

            if (_logger.isDebugEnabled())
            {
                _logger.debug("audit:result audit count is " + resultCount);
            }
            return comboCardSaleDetail;
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param comboCardSaleId
     * 
     * @return
     * 
     * @throws AlumniDatabaseException
     * 
     * @see com.bcinfo.iccard.service.ComboCardService#loadComboCardSaleDetail(long)
     */
    @Override
    public ComboCardSaleDetail loadComboCardSaleDetail(long comboCardSaleId) throws IccardDatabaseException
    {
        if (_logger.isInfoEnabled())
        {
            _logger.info("loadComboCardSaleDetail:comboCardSaleId=" + comboCardSaleId);
        }
        ComboCardSaleDetail c = null;
        try
        {
            c = _comboCardSaleDetailMapper.selectByPrimaryKey(comboCardSaleId);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
        if (c != null)
        {
            _logger.debug("load:result ComboCardSaleDetail=" + c.toString());
        }
        return c;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param twoDimensionCode
     * 
     * @param pageSize
     * 
     * @param pageNo
     * 
     * @return PaginationList<ComboCardSaleDetail>
     * 
     * @throws AlumniDatabaseException
     * 
     * @see com.bcinfo.iccard.service.ComboCardService#findComboCardSaleDetails(String,int,int)
     */
    @Override
    public PaginationList<ComboCardSaleDetail> findComboCardSaleDetails(String twoDimensionCode, int pageSize,
            int pageNo) throws IccardDatabaseException, InvalidParametersException
    {
        PaginationList<ComboCardSaleDetail> paginationList = new PaginationList<ComboCardSaleDetail>();
        List<ComboCardSaleDetail> comboCardSaleDetails = null;
        int totalLogCount = 0;
        System.out.println("findComboCardSaleDetails:twoDimensionCode=" + twoDimensionCode + ",pageSize=" + pageSize
                + ",pageNo=" + pageNo);
        if (_logger.isDebugEnabled())
        {
            _logger.info("findComboCardSaleDetails:twoDimensionCode=" + twoDimensionCode + ",pageSize=" + pageSize
                    + ",pageNo=" + pageNo);
        }
        try
        {
            totalLogCount = _comboCardSaleDetailMapper.selectCountByPage(twoDimensionCode);
        }
        catch (Exception e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
        if (pageNo <= 0 || totalLogCount < (pageNo - 1) * pageSize || pageSize <= 0)
        {
            _logger.error("findComboCardSaleDetails parameter is wrong");
            throw new InvalidParametersException();
        }
        int benginIndex = pageSize * pageNo - pageSize;
        try
        {
            comboCardSaleDetails = _comboCardSaleDetailMapper.selectByPage(twoDimensionCode, benginIndex, pageSize);
        }
        catch (Exception e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }

        int totalPage = totalLogCount % pageSize == 0 ? totalLogCount / pageSize : totalLogCount / pageSize + 1;
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageNo);
        paginationList.setPageSize(pageSize);
        paginationList.setRecords(comboCardSaleDetails);
        paginationList.setTotalRecord(totalLogCount);
        return paginationList;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param comboCardId
     * 
     * @param status
     * 
     * @param pageSize
     * 
     * @param pageNo
     * 
     * @return PaginationList<ComboCardSaleDetail>
     * 
     * @throws AlumniDatabaseException
     * 
     * @see com.bcinfo.iccard.service.ComboCardService#findGetInfo(long,int,int,int)
     */
    @Override
    public PaginationList<ComboCardSaleDetail> findGetInfo(long comboCardId, int status, int pageSize, int pageNo)
            throws IccardDatabaseException, InvalidParametersException
    {
        PaginationList<ComboCardSaleDetail> paginationList = new PaginationList<ComboCardSaleDetail>();
        List<ComboCardSaleDetail> comboCardSaleDetails = null;
        int totalLogCount = 0;

        if (_logger.isDebugEnabled())
        {
            _logger.info("findGetInfo:comboCardId=" + comboCardId + ",status=" + status + ",pageSize=" + pageSize
                    + ",pageNo=" + pageNo);
        }
        try
        {
            totalLogCount = _comboCardSaleDetailMapper.selectCountByExpensive(comboCardId, status);
        }
        catch (Exception e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
        if (pageNo <= 0 || totalLogCount < (pageNo - 1) * pageSize || pageSize <= 0)
        {
            _logger.error("findGetInfo:parameter is wrong");
            throw new InvalidParametersException();
        }
        int benginIndex = pageSize * pageNo - pageSize;
        try
        {
            comboCardSaleDetails = _comboCardSaleDetailMapper.selectByExpensive(comboCardId, status, benginIndex,
                    pageSize);
        }
        catch (Exception e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }

        int totalPage = totalLogCount % pageSize == 0 ? totalLogCount / pageSize : totalLogCount / pageSize + 1;
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageNo);
        paginationList.setPageSize(pageSize);
        paginationList.setRecords(comboCardSaleDetails);
        paginationList.setTotalRecord(totalLogCount);
        return paginationList;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param keyword
     * 
     * @param maxRows
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.service.ComboCardService#findByNameCode(java.lang.String, int)
     */
    @Override
    public List<ComboCard> findByNameCode(String keyword, int maxRows)
    {
        if (StringUtils.isBlank(keyword))
            return new ArrayList<ComboCard>();
        return _comboCardMapper.selectByName(keyword, maxRows);
    }
    
    /*
     * (non-Javadoc)
     * Description: <br/> 
     *
     * @param opId
     * @param pageSize
     * @param pageNo
     * @return
     * @throws IccardDatabaseException
     * @throws InvalidParametersException 
     * @see com.bcinfo.iccard.service.ComboCardService#findCheckAccount(long, int, int)
     */
    @Override
    public PaginationList<CheckAccountLog> findCheckAccount(long opId, int pageSize, int pageNo)
            throws IccardDatabaseException, InvalidParametersException
    {
        PaginationList<CheckAccountLog> paginationList = new PaginationList<CheckAccountLog>();
        List<CheckAccountLog> comboCardSaleDetails = null;
        int totalLogCount = 0;

        if (_logger.isDebugEnabled())
        {
            _logger.info("findCheckAccount=" + opId + ",pageSize=" + pageSize
                    + ",pageNo=" + pageNo);
        }
        try
        {
            totalLogCount = _checkAccountLogMapper.selectCountByPage(opId);
        }
        catch (Exception e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
        if (pageNo <= 0 || totalLogCount < (pageNo - 1) * pageSize || pageSize <= 0)
        {
            _logger.error("findGetInfo:parameter is wrong");
            throw new InvalidParametersException();
        }
        int benginIndex = pageSize * pageNo - pageSize;
        try
        {
            comboCardSaleDetails = _checkAccountLogMapper.selectByPage(opId, benginIndex,
                    pageSize);
        }
        catch (Exception e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }

        int totalPage = totalLogCount % pageSize == 0 ? totalLogCount / pageSize : totalLogCount / pageSize + 1;
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageNo);
        paginationList.setPageSize(pageSize);
        paginationList.setRecords(comboCardSaleDetails);
        paginationList.setTotalRecord(totalLogCount);
        return paginationList;
    }
    
    /**
     * 对账
     */
    @Override
    public int account(Date recentlyAccountDate,float accountMount,long saleUserId,long opId) throws IccardDatabaseException
    {
        if (_logger.isDebugEnabled())
        {
            _logger.debug("comboCard=" + saleUserId);
        }
        
        int result = 0;
        try
        {
            CheckAccountLog checkAccountLog = new CheckAccountLog();
            if(recentlyAccountDate!=null){
                checkAccountLog.setRecentlyAccountDate(recentlyAccountDate);
            }
            checkAccountLog.setCardType((short) 0);
            checkAccountLog.setCheckaccountMount(accountMount);
            checkAccountLog.setSaleUserId(saleUserId);
            checkAccountLog.setUserId(opId);
            result = _checkAccountLogMapper.insertSelective(checkAccountLog);
            
            _comboCardSaleDetailMapper.account(saleUserId,opId);
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new IccardDatabaseException();
        }
        return result;
    }
    
    /**
     * 
     * 一句话描述 <br/>
     * <p>Description: TODO(详细描述) 
     * <br/>
     * <p>Author: tonic<br/>
     * <p>Date: 2013-12-20-下午6:22:24<br/>
     * <p>
     * @param saleUserId
     * @param status
     * @param pageSize
     * @param pageNo
     * @return
     * @throws IccardDatabaseException
     * @throws InvalidParametersException   
     *
     */
    @Override
    public PaginationList<ComboCardSaleDetail> findAccountInfo(long saleUserId,int status, int pageSize, int pageNo)
            throws IccardDatabaseException, InvalidParametersException
    {
        PaginationList<ComboCardSaleDetail> paginationList = new PaginationList<ComboCardSaleDetail>();
        List<ComboCardSaleDetail> comboCardSaleDetails = null;
        int totalLogCount = 0;

        if (_logger.isDebugEnabled())
        {
            _logger.info("findAccountInfo:saleUserId=" + saleUserId + ",pageSize=" + pageSize
                    + ",pageNo=" + pageNo);
        }
        try
        {
            totalLogCount = _comboCardSaleDetailMapper.selectCountBySaleUserId(saleUserId, status);
        }
        catch (Exception e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
        if (pageNo <= 0 || totalLogCount < (pageNo - 1) * pageSize || pageSize <= 0)
        {
            _logger.error("findGetInfo:parameter is wrong");
            throw new InvalidParametersException();
        }
        int benginIndex = pageSize * pageNo - pageSize;
        try
        {
            comboCardSaleDetails = _comboCardSaleDetailMapper.selectBySaleUserId(saleUserId, status, benginIndex,
                    pageSize);
        }
        catch (Exception e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }

        int totalPage = totalLogCount % pageSize == 0 ? totalLogCount / pageSize : totalLogCount / pageSize + 1;
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageNo);
        paginationList.setPageSize(pageSize);
        paginationList.setRecords(comboCardSaleDetails);
        paginationList.setTotalRecord(totalLogCount);
        return paginationList;
    }
    
    /**
     * 
     */
    @Override
    public float findALLAccountBySaleUserId(long saleUserId,int status)
            throws IccardDatabaseException
    {
        float f = 0;

        if (_logger.isDebugEnabled())
        {
            _logger.info("findAccountInfo:saleUserId=" + saleUserId);
        }
        try
        {
            f = _comboCardSaleDetailMapper.findALLAccountBySaleUserId(saleUserId, status);
        }
        catch (Exception e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
        return f;
    }
    
    /**
     * 
     * 一句话描述 <br/>
     * <p>Description: TODO(详细描述) 
     * <br/>
     * <p>Author: tonic<br/>
     * <p>Date: 2013-12-24-上午11:39:36<br/>
     * <p>
     * @param cardName
     * @return
     * @throws IccardDatabaseException   
     *
     */
    @Override
    public int checkCardName(String cardName) throws IccardDatabaseException
    {
        if (_logger.isDebugEnabled())
        {
            _logger.debug("checkCardName:cardName=" + cardName);
        }
        int count = 0;
        try
        {
            count = _comboCardMapper.selectByCardName(cardName);
        }
        catch (DataAccessException e1)
        {
            _logger.error(ExceptionUtils.getStackTrace(e1));
            throw new IccardDatabaseException();
        }
        return count;
    }
    
}
